//! Message queue processor traits.

use aspect_codec::Codec;
use aspect_msgqueue::{QueueIdent, QueueNamespace};

use crate::{MsgBatch, ProcError};

/// Interface for a message queue processor.
///
/// This is for a whole protocol namespace.  We manage many queue instances
/// under a namespace.  We may call this processor from many threads in
/// parallel, so it should not directly mutate itself.
pub trait NamespaceProcessor {
    /// Data necessary to init a new instance of a queue processor.
    type InitData: Codec;

    /// The namespace that this processor serves for.
    fn namespace(&self) -> QueueNamespace;

    /// Inits a new queue in the database.  This is provided with a specific queue identifier.
    fn init_queue(&self, ident: QueueIdent, init_data: Self::InitData) -> Result<(), ProcError>;

    /// Returns the index of the last messages that we've stored that we've processed.
    fn fetch_last_processed_msg(&self, ident: QueueIdent) -> Result<u64, ProcError>;

    /// Processes a batch of messages for a particular queue.
    ///
    /// Malformed or invalid messages being provided for processing should be
    /// discarded, possibly logging a warning.  This function can return
    /// transient errors, but MUST be able to eventually continue with arbitrary
    /// message payloads.
    fn process_batch(&self, ident: QueueIdent, batch: &MsgBatch) -> Result<(), ProcError>;
}
